<?php

App::uses('AppController', 'Controller');

/**
 * Groups Controller
 *
 * @property Group $Group
 */
class QuizController  extends AppController {

    function beforeFilter() {
        $this->Auth->allow('index','confrim','block');       
        parent::beforeFilter();        
       
    }    
   
    public function index() {
    	     	
        $this->loadModel('Member'); 
       if($this->Session->read('nameMember') && $this->Session->read('nameMember')!=""){
           $this->redirect('block');
       }
       
        if ($this->request->is('post')) {
            //check ton tai dong thoi name va location
            $chek = $this->Member->find('all',array('conditions'=>array(
                'Member.name'=>$this->request->data['Member']['name'],
                'Member.location'=>$this->request->data['Member']['location']
            )));
            if(count($chek)){
                $this->Session->write('nameMember', $this->request->data['Member']['name']);
                $this->Session->write('locationMember', $this->request->data['Member']['location']);
                $this->Session->write('logTest',0);
		$this->redirect('confrim');
		 // $this->Session->write('erroMSG',"Account existed, please try again.");
            }else{
            $this->Member->create();            							
            if ($this->Member->save($this->request->data)) {
                $this->Session->write('nameMember', $this->request->data['Member']['name']);
                $this->Session->write('locationMember', $this->request->data['Member']['location']);
            	$this->Session->write('logTest',0);
                $this->Session->delete('erroMSG');
                $this->redirect('confrim');
            } else {
               
            }
            }
        }
        $location=$this->Common->getLocation();
         
        $this->set(compact('location'));
        $this->render('/Fontend/Quiz/index');        
    }
    public function confrim(){
        if(!$this->Session->read('nameMember')){
             $this->redirect('/');
        }
        if($this->Session->read('logTest')&& $this->Session->read('logTest')==1){
            $this->redirect('block');
        }
        if($this->Session->read('logTest')==2){
            $this->redirect("/tests/end");
        }
        if ($this->request->is('post')) {
            $this->Session->write('startTest', date('Y-m-d H:i:s') );
            $this->Session->write('endTest', date('Y-m-d H:i:s') );
            $this->Session->write('logTest',1 );
            $this->redirect('/tests');
        }
         $this->render('/Fontend/Quiz/confrim'); 
    }
    public function block(){
        if($this->Session->read('logTest')&& $this->Session->read('logTest')==1){           
        //tinh toan chuyen den cau hoi va bi tru so thoi gian        
         $nowTime = strtotime(date("Y-m-d H:i:s"));
         $start=strtotime(date($this->Session->read('endTest')));
         $between = $nowTime - $start;
		//$this->Session->write('orderQues',1);
			$this->Session->delete('refer');
	     $this->redirect("/tests");	
				 
       /*  if($between<30){
         
   //cau 1
            $this->Session->write('orderQues',1);
            $this->redirect("/tests");

        }elseif($between>=30 && $between<60){
            //cau 2
            $this->Session->write('orderQues',2);
            $this->redirect("/tests");
            
        }elseif($between>=60 && $between<90){
            //cau 3
           $this->Session->write('orderQues',3);
           $this->redirect("/tests");
        }elseif($between>=90 && $between<120){
            //cau 4
            $this->Session->write('orderQues',4);
            $this->redirect("/tests");
        }elseif($between>=120 && $between<150){
            //cau 5
            $this->Session->write('orderQues',5);
            $this->redirect("/tests");
        }elseif($between>=150 && $between<180){
            //cau6
            $this->Session->write('orderQues',6);
            $this->redirect("/tests");
        }else{
            //end
            $this->redirect("/tests/end");
        }
        }else{
            $this->redirect("/tests/end");
        }
        */
    }
	  $this->redirect("/tests");	

      
}
   
    

}
?>
